﻿using UnityEditor;
using UnityEditor.UI;
using UnityEngine;
using UnityEngine.UI;


[CustomEditor(typeof(InfiniteScroll))]
public class InfiniteScrollEditor : ScrollRectEditor
{
    // SerializedProperty _itemTemplate;
    private SerializedProperty _defaultItemSize;
    private SerializedProperty _layoutType;
    private SerializedProperty _spacing;
    private SerializedProperty _padding;
    private SerializedProperty _autoItemCount;
    private SerializedProperty _constraint;
    private SerializedProperty _verticalItemAlignment;
    private SerializedProperty _horizontalItemAlignment;
    private SerializedProperty _setAsSiblingType;
    
    protected override void OnEnable()
    {
        base.OnEnable();

        _defaultItemSize = serializedObject.FindProperty("defaultItemSize");
        _layoutType = serializedObject.FindProperty("layoutType");
        _spacing = serializedObject.FindProperty("spacing");
        _padding = serializedObject.FindProperty("padding");
        _autoItemCount = serializedObject.FindProperty("autoItemCount");
        _constraint = serializedObject.FindProperty("constraint");
        _verticalItemAlignment = serializedObject.FindProperty("verticalItemAlignment");
        _horizontalItemAlignment = serializedObject.FindProperty("horizontalItemAlignment");
        _setAsSiblingType = serializedObject.FindProperty("setAsSiblingType");
    }


    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        EditorGUILayout.Separator();

        EditorGUILayout.LabelField("<color=#cccccc><b>Additional configs</b></color>", new GUIStyle {richText = true});

        EditorGUI.indentLevel++;

        Vector4 padding = _padding.vector4Value;
        EditorGUILayout.LabelField("Padding");
        EditorGUI.indentLevel++;
        padding.x = EditorGUILayout.FloatField("Left", padding.x);
        padding.z = EditorGUILayout.FloatField("Right", padding.z);
        padding.y = EditorGUILayout.FloatField("Top", padding.y);
        padding.w = EditorGUILayout.FloatField("Bottom", padding.w);
        EditorGUI.indentLevel--;
        _padding.vector4Value = padding;

        EditorGUILayout.PropertyField(_defaultItemSize);
        EditorGUILayout.PropertyField(_spacing);
        _layoutType.intValue =
            (int) (InfiniteScroll.ItemLayoutType) EditorGUILayout.EnumPopup("LayoutType",
                (InfiniteScroll.ItemLayoutType) (_layoutType.intValue & 1));

        if (_layoutType.intValue == (int) InfiniteScroll.ItemLayoutType.Vertical)
        {
            _verticalItemAlignment.intValue = (int) (InfiniteScroll.VerticalItemAlignment) EditorGUILayout.EnumPopup("VerticalItemAlignment",
                (InfiniteScroll.VerticalItemAlignment) (_verticalItemAlignment.intValue));
        }
        else
        {
            _horizontalItemAlignment.intValue = (int) (InfiniteScroll.HorizontalItemAlignment) EditorGUILayout.EnumPopup("HorizontalItemAlignment",
                (InfiniteScroll.HorizontalItemAlignment) (_horizontalItemAlignment.intValue));
        }
        
        _setAsSiblingType.intValue = (int) (InfiniteScroll.ItemSetSiblingType) EditorGUILayout.EnumPopup("ItemSetSiblingType",
            (InfiniteScroll.ItemSetSiblingType) (_setAsSiblingType.intValue & 1));

        EditorGUILayout.PropertyField(_autoItemCount);
        if (!_autoItemCount.boolValue)
            EditorGUILayout.PropertyField(_constraint);

        serializedObject.ApplyModifiedProperties();

        EditorGUILayout.Separator();

        EditorGUI.indentLevel--;

        EditorGUILayout.LabelField("<color=#cccccc><b>For Original ScrollRect</b></color>",
            new GUIStyle {richText = true});

        EditorGUI.indentLevel++;
        base.OnInspectorGUI();

        EditorGUI.indentLevel--;
    }


// const string BgPath = "UI/Skin/Background.psd";
// const string SpritePath = "UI/Skin/UISprite.psd";
// const string MaskPath = "UI/Skin/UIMask.psd";
// static readonly Color PanelColor = new Color(1f, 1f, 1f, 0.392f);
// static readonly Color DefaultSelectableColor = new Color(1f, 1f, 1f, 1f);
// static readonly Vector2 ThinElementSize = new Vector2(160f, 20f);


// [MenuItem("GameObject/UI/Infinite Scroll", false, 90)]
// public static void AddScrollView(MenuCommand menuCommand)
// {
//     GameObject root = CreateUiElementRoot("Infinite Scroll", new Vector2(200, 200));
//
//     GameObject viewport = CreateUiObject("Viewport", root);
//     GameObject content = CreateUiObject("Content", viewport);
//
//     GameObject parent = menuCommand.context as GameObject;
//     if (parent != null)
//     {
//         root.transform.SetParent(parent.transform, false);
//     }
//
//     Selection.activeGameObject = root;
//
//
//     GameObject hScrollbar = CreateScrollbar();
//     hScrollbar.name = "Scrollbar Horizontal";
//     hScrollbar.transform.SetParent(root.transform, false);
//     RectTransform hScrollbarRt = hScrollbar.GetComponent<RectTransform>();
//     hScrollbarRt.anchorMin = Vector2.zero;
//     hScrollbarRt.anchorMax = Vector2.right;
//     hScrollbarRt.pivot = Vector2.zero;
//     hScrollbarRt.sizeDelta = new Vector2(0, hScrollbarRt.sizeDelta.y);
//
//     GameObject vScrollbar = CreateScrollbar();
//     vScrollbar.name = "Scrollbar Vertical";
//     vScrollbar.transform.SetParent(root.transform, false);
//     vScrollbar.GetComponent<Scrollbar>().SetDirection(Scrollbar.Direction.BottomToTop, true);
//     RectTransform vScrollbarRt = vScrollbar.GetComponent<RectTransform>();
//     vScrollbarRt.anchorMin = Vector2.right;
//     vScrollbarRt.anchorMax = Vector2.one;
//     vScrollbarRt.pivot = Vector2.one;
//     vScrollbarRt.sizeDelta = new Vector2(vScrollbarRt.sizeDelta.x, 0);
//
//
//     RectTransform viewportRect = viewport.GetComponent<RectTransform>();
//     viewportRect.anchorMin = Vector2.zero;
//     viewportRect.anchorMax = Vector2.one;
//     viewportRect.sizeDelta = Vector2.zero;
//     viewportRect.pivot = Vector2.up;
//
//     RectTransform contentRect = content.GetComponent<RectTransform>();
//     contentRect.anchorMin = Vector2.up;
//     contentRect.anchorMax = Vector2.one;
//     contentRect.sizeDelta = new Vector2(0, 300);
//     contentRect.pivot = Vector2.up;
//
//     InfiniteScroll scrollRect = root.AddComponent<InfiniteScroll>();
//     scrollRect.content = contentRect;
//     scrollRect.viewport = viewportRect;
//     scrollRect.horizontalScrollbar = hScrollbar.GetComponent<Scrollbar>();
//     scrollRect.verticalScrollbar = vScrollbar.GetComponent<Scrollbar>();
//     scrollRect.horizontalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport;
//     scrollRect.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport;
//     scrollRect.horizontalScrollbarSpacing = -3;
//     scrollRect.verticalScrollbarSpacing = -3;
//
//     Image rootImage = root.AddComponent<Image>();
//     rootImage.sprite = AssetDatabase.GetBuiltinExtraResource<Sprite>(BgPath);
//     rootImage.type = Image.Type.Sliced;
//     rootImage.color = PanelColor;
//
//     Mask viewportMask = viewport.AddComponent<Mask>();
//     viewportMask.showMaskGraphic = false;
//
//     Image viewportImage = viewport.AddComponent<Image>();
//     viewportImage.sprite = AssetDatabase.GetBuiltinExtraResource<Sprite>(MaskPath);
//     viewportImage.type = Image.Type.Sliced;
// }


// static GameObject CreateScrollbar()
// {
//     // Create GOs Hierarchy
//     GameObject scrollbarRoot = CreateUiElementRoot("Scrollbar", ThinElementSize);
//     GameObject sliderArea = CreateUiObject("Sliding Area", scrollbarRoot);
//     GameObject handle = CreateUiObject("Handle", sliderArea);
//
//     Image bgImage = scrollbarRoot.AddComponent<Image>();
//     bgImage.sprite = AssetDatabase.GetBuiltinExtraResource<Sprite>(BgPath);
//     bgImage.type = Image.Type.Sliced;
//     bgImage.color = DefaultSelectableColor;
//
//     Image handleImage = handle.AddComponent<Image>();
//     handleImage.sprite = AssetDatabase.GetBuiltinExtraResource<Sprite>(SpritePath);
//     handleImage.type = Image.Type.Sliced;
//     handleImage.color = DefaultSelectableColor;
//
//     RectTransform sliderAreaRect = sliderArea.GetComponent<RectTransform>();
//     sliderAreaRect.sizeDelta = new Vector2(-20, -20);
//     sliderAreaRect.anchorMin = Vector2.zero;
//     sliderAreaRect.anchorMax = Vector2.one;
//
//     RectTransform handleRect = handle.GetComponent<RectTransform>();
//     handleRect.sizeDelta = new Vector2(20, 20);
//
//     Scrollbar scrollbar = scrollbarRoot.AddComponent<Scrollbar>();
//     scrollbar.handleRect = handleRect;
//     scrollbar.targetGraphic = handleImage;
//     SetDefaultColorTransitionValues(scrollbar);
//
//     return scrollbarRoot;
// }


// private static GameObject CreateUiElementRoot(string name, Vector2 size)
// {
//     GameObject child = new GameObject(name);
//     RectTransform rectTransform = child.AddComponent<RectTransform>();
//     rectTransform.sizeDelta = size;
//     return child;
// }
//
// private static GameObject CreateUiObject(string name, GameObject parent)
// {
//     GameObject go = new GameObject(name);
//     go.AddComponent<RectTransform>();
//     SetParentAndAlign(go, parent);
//     return go;
// }

// private static void SetParentAndAlign(GameObject child, GameObject parent)
// {
//     if (parent == null)
//         return;
//
//     child.transform.SetParent(parent.transform, false);
//     SetLayerRecursively(child, parent.layer);
// }

// static void SetLayerRecursively(GameObject go, int layer)
// {
//     go.layer = layer;
//     Transform t = go.transform;
//     for (int i = 0; i < t.childCount; i++)
//         SetLayerRecursively(t.GetChild(i).gameObject, layer);
// }
//
// static void SetDefaultColorTransitionValues(Selectable slider)
// {
//     ColorBlock colors = slider.colors;
//     colors.highlightedColor = new Color(0.882f, 0.882f, 0.882f);
//     colors.pressedColor = new Color(0.698f, 0.698f, 0.698f);
//     colors.disabledColor = new Color(0.521f, 0.521f, 0.521f);
// }
}